﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using control.Biz;
using System.Collections;

namespace control.Dal
{
    class DalTonKho
    {
        internal static int GetLastByIDSach(int p)
        {

            Sach_WebDataContext db = DalCommon.SachDB;
            var tks = from t in db.TonKhos
                      where t.IDSach == p
                      orderby t.ThoiGian descending
                      select new BizTonKho()
                      {
                          ID = t.ID,
                          IDSach = t.IDSach,
                          SoLuong = (int)t.SoLuong,
                          ThoiGian = (DateTime)t.ThoiGian
                      };
            int resuilt = 0;

            try { resuilt = tks.FirstOrDefault<BizTonKho>().SoLuong; }
            catch { }
            return resuilt; 
        }

        internal static int Insert(BizTonKho bizTonKho)
        {
            int saiso = bizTonKho.SoLuong - GetLastByIDSach(bizTonKho.IDSach, bizTonKho.ThoiGian);
            UpdateKho(bizTonKho.IDSach, bizTonKho.ThoiGian, saiso);
            Sach_WebDataContext db = DalCommon.SachDB;
            TonKho tonkho = new TonKho()
            {
                IDSach = bizTonKho.IDSach,
                SoLuong = bizTonKho.SoLuong,
                ThoiGian = bizTonKho.ThoiGian,
            };
            db.TonKhos.InsertOnSubmit(tonkho);
            db.SubmitChanges();
            return tonkho.ID;
        }

        internal static int GetLastByIDSach(int p, DateTime dateTime)
        {
            Sach_WebDataContext db = DalCommon.SachDB;
            var tks = from t in db.TonKhos
                      where t.IDSach == p &&dateTime.CompareTo((DateTime)t.ThoiGian)>0
                      orderby t.ThoiGian descending
                      select new BizTonKho()
                      {
                          ID = t.ID,
                          IDSach = t.IDSach,
                          SoLuong = (int)t.SoLuong,
                          ThoiGian = (DateTime)t.ThoiGian
                      };
            double resuilt = 0;

            try { resuilt = tks.FirstOrDefault<BizTonKho>().SoLuong; }
            catch { }
            return (int)resuilt; 
        }

        internal static void DeleteByIDSach_TG(int p, DateTime dateTime)
        {
            try
            {
                Sach_WebDataContext db = DalCommon.SachDB;
                TonKho tonkho = db.TonKhos.Single(t => (t.IDSach == p && dateTime.CompareTo((DateTime)t.ThoiGian) == 0));
                int LastSoluong = GetLastByIDSach(p, dateTime);
                int saiso = (int)tonkho.SoLuong - LastSoluong;
                UpdateKho(p, dateTime, -saiso);
                db.TonKhos.DeleteOnSubmit(tonkho);
                db.SubmitChanges();
            }
            catch
            {
                
            }
        }

        private static void UpdateKho(int p, DateTime dateTime, int saiso)
        {
            Sach_WebDataContext db = DalCommon.SachDB;
            var tonkhos = from tk in db.TonKhos
                          where tk.IDSach == p && dateTime.CompareTo((DateTime)tk.ThoiGian) < 0
                          select tk;
            List<TonKho> listtonkho = tonkhos.ToList<TonKho>();
            if (listtonkho.Count != 0)
            {
                foreach (TonKho item in listtonkho)
                {
                    item.SoLuong += saiso;
                }
            }
            db.SubmitChanges();
        }

        internal static List<BizTonKho> Find(string Sach, DateTime newFrom, DateTime newTo)
        {
            var tonkhos = from t in DalCommon.SachDB.TonKhos
                          where (t.Sach.MaSach.Contains(Sach) || t.Sach.TenSach.Contains(Sach)) && newFrom.CompareTo(t.ThoiGian) <= 0 && newTo.CompareTo(t.ThoiGian) >= 0
                          select new BizTonKho()
                          {
                              ID = t.ID,
                              IDSach = t.Sach.IDSach,
                              SoLuong = (int)t.SoLuong,
                              ThoiGian = (DateTime)t.ThoiGian,
                          };
            return tonkhos.ToList<BizTonKho>();
        }
    }
}
